package com.chuangyang.fixboxmaster.ui.fragment.repair;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.utils.LocationUtils;
import com.chuangyang.fixboxlib.widgets.PullDownRefreshListView;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.NewOrder;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.adapter.RepairListAdapter;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;
import com.chuangyang.fixboxmaster.ui.fragment.OrderListFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


/**
 * Created by vanshu on 15/3/18 00:03.
 * 可接订单页面
 */
public class RepairListFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullDownRefreshListView.OnRefreshListener {
    private static final String TAG = OrderListFragment.class.getSimpleName();
    private View mContentView;
    private RepairListAdapter mAdapter;
    private ArrayList<NewOrder> mOrderList = new ArrayList<>();
    private PullDownRefreshListView mListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.repair_list_view, container, false);
        mListView = (PullDownRefreshListView) mContentView.findViewById(R.id.listview);
        mAdapter = new RepairListAdapter(mOrderList);
        mListView.setAdapter(mAdapter);
        mListView.setOnRefreshListener(this);
        mListView.setOnItemClickListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
    }


    public void loadData() {
        mListView.setLoading();
        loadDataWithoutFootLoading();
    }

    public void loadDataWithoutFootLoading() {
        HashMap<String, String> param = new HashMap<>();
        param.put("pageIndex", String.valueOf(mOrderList.size()));
        param.put("latitude", String.valueOf(LocationUtils.latitude));
        param.put("longitude", String.valueOf(LocationUtils.longitude));
        addRequest(new GsonRequest<>(FixBoxApi.GET_ALL_UNTAKE_ORDERS, param, NewOrder.NewOrderInfo.class, responseListener));
    }


    private Response.Listener<NewOrder.NewOrderInfo> responseListener = new Response.Listener<NewOrder.NewOrderInfo>() {
        @Override
        public void onResponse(NewOrder.NewOrderInfo response) {
            if (response.result == null || response.result.orderList==null|| response.result.orderList.length == 0) {
                setEmptyView();
                return;
            }
            mOrderList.addAll(Arrays.asList(response.result.orderList));
            mAdapter.notifyDataSetChanged();
            showResult();
            if (mOrderList.size() >= response.result.totalCount)
                mListView.setLoadingEnd();
            else
                mListView.setLoadingIdle();
        }
    };


    public void updateListView() {
        mOrderList.clear();
        mListView.setAdapter(mAdapter);
        loadDataWithoutFootLoading();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent orderDetailIntent = new Intent(getActivity(), ModuleActivity.class);
        if (mOrderList.get(position).appType.equals("1000")) { //1000表示企业订单
            orderDetailIntent.putExtra(ModuleActivity.TYPE, ModuleActivity.B2B_ORDER_DETAL_ACTIVITY);
            orderDetailIntent.putExtra(ModuleActivity.ORDER_ID, String.valueOf(mOrderList.get(position).id));
        } else  {
            orderDetailIntent.putExtra(ModuleActivity.TYPE, ModuleActivity.ORDER_DETAL_ACTIVITY);
            orderDetailIntent.putExtra(ModuleActivity.ORDER_ID, String.valueOf(mOrderList.get(position).id));
            orderDetailIntent.putExtra(ModuleActivity.TRANSCATION_ID, String.valueOf(mOrderList.get(position).transactionId));

        }
        startActivity(orderDetailIntent);
    }


    public ListView getListView() {
        return mListView;
    }

    @Override
    public void onRefresh() {
        loadData();
    }

}
